<!-- 选择物料 -->
<template>
	<view class="ku-content ku-flex-button-content onScroll">
		<view class="">
			<u-cell-group>
				<u-cell icon="file-text" :title="QRcodeNumber">
					<view slot="value">
						<u-button type="primary" text="扫码" @click="scanCodeClick"></u-button>
					</view>
				</u-cell>
			</u-cell-group>

		</view>
		<view class="ku-section" v-if="shippingNoticeList.length > 0">
			<view class="ku-card">
				<view class="ku-subtitle">
					订单编号：{{shippingNoticeList[0].f_ryjf_xsdh}}
				</view>
				<view class="ku-subtitle">
					客户：{{shippingNoticeList[0].fcustname}}
				</view>
			</view>

			<view class="ku-card" v-for="(item,index) in shippingNoticeList" :key="index"
				:class="item.fentryid===currentNoticeInfo.fentryid?'active':'' " @click="onclickNoticeItem(index)">
				<view class="ku-subtitle">
					{{item.fnumber}}【{{item.fname}}】
				</view>
				<view class="">
					<u-row justify="space-between" gutter="10">
						<u-col span="12">
							<view class="ku-mi-title">生产订单号：{{ item.f_ryjf_xsdh }}</view>
						</u-col>
					</u-row>
					<u-row justify="space-between" gutter="10">
						<u-col span="12">
							<view class="ku-mi-title">{{processingDisplayContentFun(item.color)}}/
							{{processingDisplayContentFun(item.size)}}/{{processingDisplayContentFun(item.doorwidth)}}</view>
						</u-col>
						<!-- <u-col span="6">
							<view class="ku-mi-title">0000000320测试仓库/AT001仓位</view>
						</u-col> -->
					</u-row>
				</view>
				<view class="">
					<u-row justify="space-between" gutter="10">
						<u-col span="5">
							<view class="ku-button ku-text-center ku-mini-button">
								{{item.fremainoutqty}}/{{item.fqty}} 米
							</view>
						</u-col>
						<u-col span="7">
							<view class="ku-xs-title ku-text-right">
								要货日期：{{formatDateDayFun(item.fdeliverydate)}}
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
		</view>

		<u-empty v-else mode="data" marginTop='40px'>
		</u-empty>
		<view class="ku-flex-body">
			<view class="ku-section">
				<view class="" v-if="currentNoticeInfo.fentryid">
					{{currentNoticeInfo.fnumber}}【{{currentNoticeInfo.fname}}】
				</view>
				<view class="" v-else>
					请选择发货通知单
				</view>
				<view class="ku-operation-button">
					<u-button type="primary" shape='circle' color="linear-gradient(to right,#235EFF, #235EFF)"
						:disabled="currentDisabledState" text="下一步" @click="toOutboundDetails"></u-button>
				</view>
			</view>
		</view>
		<u-modal :show="modalShow" :title="modalTitle" @confirm="confirmCloseModal" :closeOnClickOverlay="true">
			<view class="ku_slot_modal">
				<view class="ku_slot_content">
					{{ modalContent }}
				</view>
			</view>
		</u-modal>
		<u-notify ref="uNotify"></u-notify>
	</view>
</template>

<script>
	import store from "@/store";
	import Dayjs from "dayjs";
	import {
		getQRcoedShippingNoticeList,
		login,
		pushSalesOutbound
	} from "@/api/index.js";
	import {
		phoneCameraScanCode,
		processingDisplayContent
	} from "@/utils/public.js";

	export default {
		data() {
			return {
				value6: '2',
				QRcodeNumber: store.state.QRcodeNumber, // 二维码单号
				shippingNoticeList: [], // 根据单据编号查询发货通知单
				currentNoticeInfo: {
					fentryid: ''
				}, // 当前用户选的通知单详情
				currentDisabledState: true,

				pushSalesOutboundInfo: '', // 下推销售出库详情
				modalShow: false, // 提示框
				modalTitle: '',
				modalContent: '',

				callScanDelayedClosureState: true, // 扫码延迟关闭
				showIndex: null
			};
		},
		/*
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			let _that = this;
			// 当前页面参数
			// this.initEmitMonitor();
			if (!this.$globalConfigs.IS_DEV) {
				window.callScanBack = this.callScanBack;
			}else{
				setTimeout(function() {
					_that.scanCodeClick();
				}, 500);
			}
			
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {

		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		methods: {
			// 扫码方法--公共
			async scanCodeClick() {
				console.log(this.$globalConfigs.IS_DEV);
				if (this.$globalConfigs.IS_DEV) {
					await phoneCameraScanCode(['barCode', 'qrCode'], true).then((res) => {
						console.log('扫码方法--公共--res-->', res);
						if (res.state === '0000') {
							this.QRcodeNumber = res.result;
							this.$nextTick(() => {
								this.getQRcoedShippingNoticeListFun();
							})
						}
					});

				} else {
					this.callScanNative();
				}

			},
			// 原生红外扫码
			callScanNative() {
				let _that = this;
				window.android.callScan();
				uni.showLoading({
					title: '扫码中...'
				});
			},
			//扫码数据回调
			callScanBack(res) {
				console.log('扫码数据回调----->', res);
				if (typeof res === 'string') {
					try {
						let result = {};
						result = JSON.parse(res);
						console.log('扫码数据回调----->', result);
						if (result.status_code === 200 && result.code) {
							this.QRcodeNumber = result.code;
							this.$nextTick(() => {
								this.getQRcoedShippingNoticeListFun();
							})
						} else if (result.status_code === 204) {
							this.$refs.uNotify.warning('请重新扫码！');
						}
					} catch (err) {
						//TODO handle the exception
						this.$refs.uNotify.warning('扫码错误请重新扫码！');
					}
				}
				uni.hideLoading();
			},



			toOutboundDetails() {
				console.log('当前选择的出库单信息-----》', this.currentNoticeInfo)
				let params = {
					fid: this.currentNoticeInfo.fid,
					fentryid: this.currentNoticeInfo.fentryid,
					f_ryjf_pis: 0,
				};
				pushSalesOutbound(params).then((res) => {
					console.log('发货通知单下推销售出库---res-->', res)
					// 0是下推失败 ，其它数字为下推成功，继续操作
					this.pushSalesOutboundInfo = res;
					if (res.resp_code === 0) {
						this.$navTo('pages/warehouse/outboundDetails', {
							deliveryNoteID: this.pushSalesOutboundInfo.datas,
							index: String(this.showIndex)
						})
					} else if (res.resp_code === 1) {
						// resp_code == 1
						if (res.datas && res.datas !== '0') {
							this.modalShow = true;
							this.modalTitle = "提示";
							this.modalContent = res.resp_msg;
						} else {
							this.$refs.uNotify.warning('下推失败！请联系管理员')
						}
					}
				})
			},
			// 确认关闭弹出框
			confirmCloseModal() {
				this.modalShow = false;
				this.$navTo('pages/warehouse/outboundDetails', {
					deliveryNoteID: this.pushSalesOutboundInfo.datas,
					index: String(this.showIndex)
				})
			},


			getQRcoedShippingNoticeListFun() {
				let _that = this;
				let params = {
					fnumber: this.QRcodeNumber,
				};
				getQRcoedShippingNoticeList(params).then((res) => {
					_that.callScanDelayedClosureState = true;
					console.log('获取二维码对应的单据--根据单据编号查询发货通知单---res-->', res)
					if (res.code === 200 && res.datas) {
						this.shippingNoticeList = res.datas || [];
						if (this.shippingNoticeList.length === 0) {
							this.$refs.uNotify.warning('暂未找到发货通知单！');
						}
					} else {}
				})
			},
			// 选择通知单
			onclickNoticeItem(index) {
				console.log('选择通知单------->', index);
				console.log('选择通知单------->', this.shippingNoticeList[index]);
				
				this.showIndex = index;
				this.currentNoticeInfo = this.shippingNoticeList[index];
				store.state.deliveryNoteInfo = this.currentNoticeInfo
				this.currentDisabledState = false;
			},
			// 处理内容显示内容
			processingDisplayContentFun(val) {
				return processingDisplayContent(val);
			},
			// 日期格式化
			formatDateDayFun(val) {
				if (val) {
					return Dayjs(val).format('YYYY-MM-DD HH:mm:ss');
				} else {
					return '';
				}
			
			},

		},
		/**
		   * 
		''   * 下拉刷新
		   */
		onPullDownRefresh() {
			this.getQRcoedShippingNoticeListFun();
		},

		/**
		 * 分享当前页面
		 */
		onShareAppMessage() {},

		/**
		 * 分享到朋友圈
		 * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
		 * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
		 */
		onShareTimeline() {},
	}
</script>

<style lang="scss" scoped>
.onScroll{
	min-height: 100vh;
	height: auto !important;
	overflow: initial !important;
}
</style>